package com.servlet.contacts;

import java.io.IOException;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.TreeSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.listener.ContextListener;
import com.servlet.model.base.Contact;
import com.servlet.model.base.News;
import com.servlet.model.base.Profile;
import com.servlet.model.database.BaseAcces;

/**
 * Servlet appelée lors de l'acceptation d'un contact dans la page de news
 * @author Marc Bouffard
 */
public class AccepterContact extends HttpServlet
{	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{	
		HttpSession session = request.getSession();
		
		BaseAcces base = ContextListener.base;
		String email = (String) session.getAttribute("email");

		int res = Integer.parseInt( request.getParameter("res") );
		String dest = request.getParameter("dest" );
		ArrayList<Contact> contacts = base.findContacts(email);
		
		Contact c = null;
		
		if( dest != null )
		{
			for( int i = 0 ; i < contacts.size() ; i++ )
			{
				if( contacts.get(i).getEmail_user2().compareTo( email ) == 0 &&
						contacts.get(i).getEmail_user1().compareTo( dest ) == 0 &&
						!contacts.get(i).getIs_accepted() )
				{
					c = contacts.get(i);
				}
					
			}
			
			if( c != null )
			{
				ArrayList<News> news = base.findNews(email);
				News n = null;
				
				for( int i = 0 ; i < news.size() ; i++ )
				{
					if( news.get(i).getEmail_user2().compareTo( email ) == 0 &&
							news.get(i).getEmail_user1().compareTo( dest ) == 0 &&
							news.get(i).getGenre() == 1 )
					{
						n = news.get(i);
					}
						
				}
				
				if( n!= null )
					base.removeNews(n);
				
				if( res == 0 )
				{
					base.removeContact(c);
				}
				else if( res == 1 )
				{
					c.setIs_accepted(true);
					
					java.util.Date dd = Calendar.getInstance().getTime();
					DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
					String dat = dateFormat.format(dd);
					Date date = Date.valueOf(dat);
					
					c.setDate_acceptation(date);
					
					for( int i = 0 ; i < contacts.size() ; i++ )
					{
						if( contacts.get(i).getEmail_user1().compareTo(email) == 0 )
							base.addNews( email , contacts.get(i).getEmail_user2() , date , 3 , dest );
						else
							base.addNews( email , contacts.get(i).getEmail_user1() , date , 3 , dest );
					}
					
					ArrayList<Contact> contacts2 = base.findContacts(dest);
					
					for( int i = 0 ; i < contacts2.size() ; i++ )
					{
						if( contacts2.get(i).getEmail_user1().compareTo(dest) == 0 )
							base.addNews( dest , contacts2.get(i).getEmail_user2() , date , 3 , email );
						else
							base.addNews( dest , contacts2.get(i).getEmail_user1() , date , 3 , email );
					}
					
					base.addNews( dest , email , date , 2 );
					
					base.addNews( email , dest , date , 2 );
					
					base.updateContact(c);
				}
			}
		}
		response.sendRedirect("news?indice=" + session.getAttribute("numero_page"));
	}
}

